// myPages/discover/discover.js
const util = require('../../utils/util.js');
const api = require('../../config/api.js');

const app = getApp()
Page({
  data: {
    teacher: {},
    teacherId: -1,
    userId: -1,
    question: ''
  },
  getVal: function(e) {
    this.setData({
      question: e.detail.value
    })
  },
  getIndexData: function() {
    let that = this;
    util.request(api.TutorInfo, {
      userId: this.data.userId,
      tutorId: this.data.teacherId
    }, 'POST').then(function(res) {
      if (res.code === 200) {
        //添加设置收藏
        that.setData({
          teacher: res.result,
        })
      }
    });
  },

  goToTeacher: function() {
    let that = this
    if (that.data.teacherId != undefined) {
      wx.navigateTo({
        url: '../../myPages/teacher/teacher?teacherId=' + that.data.teacherId
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    that.setData({
      teacherId: options.teacherId
    });
    var userId = wx.getStorageSync('userInfo').id //获取用户id
    if (userId == null || userId == undefined) {
      wx.navigateTo({
        url: "../authorize/authorize"
      })
    } else {
      that.setData({
        userId: userId
      })
    }
  },
  // 支付跳转
  goPay: function() {
    let val = this.data.question;
    //校验用户手机号码
    var phoneNum = wx.getStorageSync('userInfo').phoneNum
    if (!phoneNum) {
      //跳转到验证手机号页面
      wx.navigateTo({
        url: '../../myPages/mobileChecking/mobileChecking?type=fastAak'
      })
      return
    }
    if (!val) {
      wx.showToast({
        title: "问题为必填项",
        icon: 'none'
      });
      return false;
    } else {
      let data = {}
      let fastAskOrder = {}
      let openid = wx.getStorageSync('userInfo').openid;
      fastAskOrder.userId = this.data.userId
      fastAskOrder.tutorId = this.data.teacherId
      fastAskOrder.question = this.data.question
      fastAskOrder.price = this.data.teacher.fastAskPrice
      data.fastAskOrder = fastAskOrder
      data.openid = openid
      util.request(api.FastAskOrderPay, data, 'POST', true).then(function (res) {
        if (res.code === 200) {
          let payParam = res.result;
          console.log(payParam);
          wx.requestPayment({
            'timeStamp': payParam.timeStamp,
            'nonceStr': payParam.nonceStr,
            'package': payParam.package,
            'signType': 'MD5',
            'paySign': payParam.sign,
            'success': function (res) {
              console.log(res)
              wx.navigateTo({
                url: '../../myPages/payFastAskSuccess/payFastAskSuccess?price=' + payParam.fastAskOrder.price + '&fastAskOrderId=' + payParam.fastAskOrder.id
              })
            },
            'fail': function (res) {
              console.log(res)
              wx.showToast({
                title: '付款失败：res',
                icon: 'none'
              });
            }
          })
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none'
          });
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getIndexData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let that = this;
    return {
      title: '知识鸭', // 转发后 所显示的title
      path: '/pages/discover/discover', // 相对的路径
      imageUrl: "/images/common/share_app.png",
      success: (res) => { // 成功后要做的事情
        wx.getShareInfo({
          shareTicket: res.shareTickets[0],
          success: (res) => {},
          fail: function(res) {
            console.log(res)
          },
          complete: function(res) {
            console.log(res)
          }
        })
      },
      fail: function(res) {
        // 分享失败
        console.log(res)
      }
    }
  },
})